<!--
 * @Description:
 * @Version: 1.0
 * @Author: liguansheng
 * @Date: 2022-08-24 15:54:09
 * @LastEditors: liguansheng
 * @LastEditTime: 2022-08-30 14:00:26
-->
<template>
  <div>
    <el-form
      :model="form"
      :rules="rules"
      ref="formElRef"
      label-width="80px"
      label-position="top"
      inline
      class="setRestrictionsForm"
    >
      <el-form-item
        label="名称"
        prop="provider"
      >
        <el-input v-model="form.provider" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          :icon="Plus"
          @click="handleAdd"
        >
          添加一个新的
        </el-button>
      </el-form-item>
    </el-form>
    <el-form
      :model="form2"
    >
      <el-form-item
        label=""
        prop="type"
        style="width:100%"
      >
        <el-checkbox
          label="启用本地登录"
          v-model="form2.enableLocalLogin"
        />
      </el-form-item>
    </el-form>
    <ul
      class="restrictions"
      v-if="list.length"
    >
      <li class="title">
        <span class="provider">名称</span>
      </li>
      <li
        class="secret"
        v-for="(item, index) in list"
        :key="index"
      >
        <span class="provider">{{ item }}</span>
        <el-button
          :icon="Delete"
          type="danger"
          size="small"
          @click="handleDelete(index)"
        />
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { PropType, ref, watch } from 'vue'
import { Plus, Delete } from '@element-plus/icons-vue'
import { IElForm, IElFormRule } from '@/types/element-plus'

interface IFormData{
  provider: string
}
const props = defineProps({
  restrictions: {
    type: Array as PropType<string[]>,
    required: true
  },
  enableLocalLogin: {
    type: Boolean,
    required: true
  }
})
interface EmitsType {
  (e: 'update:restrictions', restrictions: string[]):void
  (e: 'update:enableLocalLogin', enableLocalLogin: boolean):void
}
const emit = defineEmits<EmitsType>()

const formElRef = ref<IElForm|null>(null)

const form = ref<IFormData>({
  provider: ''
})
const form2 = ref({
  enableLocalLogin: props.enableLocalLogin
})
const rules = ref<IElFormRule>({
  provider: [{ required: true, message: '字段不可为空', trigger: 'blur' }]
})

const list = ref<string[]>(props.restrictions)
watch(list, (newValue) => {
  emit('update:restrictions', newValue)
}, {
  deep: true
})
watch(() => props.restrictions, (newValue) => {
  list.value = newValue
}, {
  deep: true
})

watch(form2, (newValue) => {
  emit('update:enableLocalLogin', newValue.enableLocalLogin)
})
watch(() => props.enableLocalLogin, (newValue) => {
  form2.value.enableLocalLogin = newValue
})

const handleAdd = async () => {
  await formElRef.value?.validate((valid) => {
    if (valid) {
      list.value.push(form.value.provider)
      form.value = {
        provider: ''
      }
    }
  })
}
const handleDelete = (index: number) => {
  list.value.splice(index, 1)
}
</script>

<style scoped lang="scss">
@import '@/styles/index.scss';

.setRestrictionsForm{
  .el-form-item{
    width: 100%;
    margin-bottom: 20px;
  }
}
.restrictions{
  span{
    display: inline-block;
    line-height: 1.5;
    font-size: 16px;
  }
  .title{
    font-weight: 600;
  }
  .secret{
    height: 40px;
    display: flex;
    align-items: center;
    &:nth-child(2n){
      background-color:  #e9e9eb;
    }
  }
  .provider{
    width: 94%;
    padding-left: 10px;
    @include ellipsis(1);
  }
}
</style>
